package com.lanhai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * <p>
 * Title: DateUtil
 * </p>
 * <p>
 * Description: 提供日期有关的相关操�?
 * </p>
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company: 福建电信科学�?术研究院有限公司
 * </p>
 * 
 * @author 陈宓
 * @version 1.0
 */
public class DateUtil
{
	/**
	 * 根据传入的模式参数返回当天的日期
	 * 
	 * @param pattern 传入的模�?
	 * @return 按传入的模式返回�?个字符串
	 */
	public static String getToday(String pattern)
	{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 比较两个日期大小
	 * 
	 * @param date1 日期字符�?
	 * @param pattern1 日期格式
	 * @param date2 日期字符�?
	 * @param pattern2 日期格式
	 * @return boolean 若是date1比date2小则返回true
	 * @throws ParseException
	 */
	public static boolean compareMinDate(String date1, String pattern1,
		String date2, String pattern2)
		throws ParseException
	{
		Date d1 = convertToCalendar(date1, pattern1).getTime();
		Date d2 = convertToCalendar(date2, pattern2).getTime();
		return d1.before(d2);
	}

	/**
	 * 根据传入的日期字符串以及格式，产生一个Calendar对象
	 * 
	 * @param date 日期字符�?
	 * @param pattern 日期格式
	 * @return Calendar
	 * @throws ParseException 当格式与日期字符串不匹配时抛出该异常
	 */
	public static Calendar convertToCalendar(String date, String pattern)
		throws ParseException
	{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date d = sdf.parse(date);
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(d);
		return calendar;
	}

	/**
	 * 用�?�：以指定的格式格式化日期字符串
	 * 
	 * @param pattern 字符串的格式
	 * @param currentDate 被格式化日期
	 * @return String 已格式化的日期字符串
	 * @throws NullPointerException 如果参数为空
	 */
	public static String formatDate(Calendar currentDate, String pattern)
	{
		if (currentDate == null || pattern == null)
		{
			throw new NullPointerException("The arguments are null !");
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = currentDate.getTime();
		return sdf.format(date);
	}

	/**
	 * 用�?�：以指定的格式格式化日期字符串
	 * 
	 * @param pattern 字符串的格式
	 * @param currentDate 被格式化日期字符�? 必须为yyyymmdd
	 * @return String 已格式化的日期字符串
	 * @throws NullPointerException 如果参数为空
	 * @throws java.text.ParseException 若被格式化日期字符串不是yyyymmdd形式时抛�?
	 */
	public static String formatDate(String currentDate, String pattern)
		throws java.text.ParseException
	{
		if (currentDate == null || pattern == null)
		{
			throw new NullPointerException("The arguments are null !");
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(currentDate);
		sdf.applyPattern(pattern);
		return sdf.format(date);
	}

	/**
	 * 用�?�：以指定的格式格式化日期字符串
	 * 
	 * @param formator 格式字符�?
	 * @param currentDate 被格式化日期字符�? 必须为yyyymmdd
	 * @return String 已格式化的日期字符串
	 * @throws NullPointerException 如果参数为空
	 * @throws java.text.ParseException 若被格式化日期字符串不是yyyymmdd形式时抛�?
	 */
	public static Calendar strToDate(String strDate, String formator)
	{
		if (strDate == null || formator == null)
		{
			throw new NullPointerException("The arguments are null !");
		}

		Calendar date = Calendar.getInstance();
		date.clear();
		date.setTime(java.sql.Date.valueOf(strDate));
		return date;
	}

	/**
	 * 用�?�：以指定的格式格式化日期字符串
	 * 
	 * @param pattern 字符串的格式
	 * @param currentDate 被格式化日期
	 * @return String 已格式化的日期字符串
	 * @throws NullPointerException 如果参数为空
	 */
	public static String formatDate(Date currentDate, String pattern)
	{
		if (currentDate == null || pattern == null)
		{
			throw new NullPointerException("The arguments are null !");
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(currentDate);
	}

	/**
	 * 将指定格式的时间String转为Date类型
	 * 
	 * @param dateStr String 待转换的时间String
	 * @param pattern String 转换的类�?
	 * @throws ParseException
	 * @return Date
	 */
	public static Date convertStringToDate(String dateStr, String pattern)
		throws ParseException
	{
		if (dateStr == null)
		{
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(dateStr);
	}
	/**
	 * 用�?�：以指定的格式格式化日期字符串
	 * 
	 * @param formator 格式字符�?
	 * @return String 已格式化的日期字符串
	 */
	public static String getFormatPatternDate(String src,String pattern){
		String result = "";
		SimpleDateFormat sdf1 = null;
		SimpleDateFormat sdf2 = null;
		if("yyyyMMddHHmmss".equals(pattern)){
			sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		if("yyyyMMdd".equals(pattern)){
			sdf1 = new SimpleDateFormat("yyyyMMdd");
			sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		}
		if("yyyy-MM-dd HH:mm:ss".equals(pattern)){
			sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
			sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		if("yyyy-MM-dd".equals(pattern)){
			sdf2 = new SimpleDateFormat("yyyyMMdd");
			sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		}
		try {
			result = sdf2.format(sdf1.parse(src));
		} catch (ParseException e) {
			//e.printStackTrace();
			return src;
		}
		return result;
	}
	
	/**
	 * 获得当日零时零分零秒的DATE
	 * @param date
	 * @return
	 */
	public static Date getDayStart(Date date) {
		GregorianCalendar calendar = new GregorianCalendar();		
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	
	
	/**
	 * 日期相加�?
	 * @param date
	 * @return
	 */
	public static Date addDate(Date date, int days) {
		GregorianCalendar calendar = new GregorianCalendar();		
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, days);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	

	public static Date getMonthStart(int year, int month) {
		GregorianCalendar calendar = new GregorianCalendar();		
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	
	/**
	 * 日期相加�?
	 * @param date
	 * @return
	 */
	public static Date addMonth(Date date, int months) {
		GregorianCalendar calendar = new GregorianCalendar();		
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, months);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
	}

}
